const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const strlen = str => {
  var len = 0;
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    //单字节加1
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
      len++;
    }
    else {
      len += 2;
    }
  }
  return len;
}

const strmask = (str, index) => {
  var len = 0;
  var retstr = '';
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
      len++;
    }
    else {
      len += 2;
    }
    retstr += str.charAt(i);
    if (len > index) {
      retstr += '...';
      break;
    }
  }
  return retstr;
}

const isEmpty = str => {
  var ret = false;
  if (typeof (str) == "undefined") {
    ret = true;
  }
  if (str == null) {
    ret = true;
  }
  if (str != null && str.length == 0) {
    ret = true;
  }
  return ret;
}

module.exports = {
  formatTime: formatTime,
  strlen: strlen,
  strmask: strmask,
  isEmpty: isEmpty
}
